package com.zyx.flinkdemo.stream.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zyx
 * @since 2021/7/16 22:25
 * desc: Common工具类
 */
public class CommonUtils {
    public static <T> List<String[]> listToStringArrayList(List<T> list, Class<T> tClass) {
        // 取出list中的元素并添加到字符串数组中
        Field[] fields = tClass.getDeclaredFields();
        List<String[]> resList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            try {
                int listSize = list.size();
                for (Field field : fields) {
                    field.setAccessible(true);
                    String[] strArray = new String[listSize];
                    for (int j = 0; j < listSize; j++) {
                        Object obj = field.get(list.get(j));
                        strArray[j] = obj == null ? "" : obj.toString();
                    }
                    resList.add(strArray);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            for (int i = 0; i < fields.length; i++) {
                String[] init = {};
                resList.add(init);
            }
        }
        return resList;
    }

    public static void main(String[] args) {
        String[] strs = {};
        System.out.println(strs.length);
    }
}
